package com.shg.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
@Api(value = "文件上传接口",tags = "文件上传接口")
@Controller
public class FormTestController {

    @GetMapping(value = "/form_layouts")
    public String form_layouts() {
        return "form/form_layouts";
    }

    /**
     * 文件上传
     * <p>
     * 1. MultipartFile对象会自动封装上传的文件
     *
     * @param email
     * @param username
     * @param headerImg
     * @param photos
     * @return
     */
    @PostMapping(value = "/upload")
    @ApiOperation(value = "文件上传测试")
    public String uploadFile(@ApiParam(value = "邮箱") @RequestParam(value = "email") String email,
                             @ApiParam(value = "用户名") @RequestParam(value = "username") String username,
                             @ApiParam(value = "头像") @RequestPart(value = "headerImg") MultipartFile headerImg,
                             @ApiParam(value = "生活照") @RequestPart(value = "photos") MultipartFile[] photos) throws IOException {

        log.info("上传的信息：邮箱={},用户名={},头像={},生活照={}", email, username, headerImg.getSize(), photos.length);
        if (!headerImg.isEmpty()) {
            // todo 保存到文件服务器,如阿里云的OSS中
            headerImg.transferTo(new File("D:\\a_myFile\\person_file\\tempFile\\" + headerImg.getOriginalFilename()));
        }
        if (photos.length > 0) {
            for (MultipartFile photo : photos) {
                if (!photo.isEmpty()) {
                    String originalFilename = photo.getOriginalFilename();
                    photo.transferTo(new File("D:\\a_myFile\\person_file\\tempFile\\" + originalFilename));
                }
            }
        }
        return "main";
    }

    @PostMapping(value = "/upload_1")
    @ApiOperation(value = "文件上传测试1")
    public String uploadFile_1(@ApiParam(value = "邮箱") @RequestParam(value = "email") String email,
                               @ApiParam(value = "用户名") @RequestParam(value = "username") String username,
                               @ApiParam(value = "头像") @RequestParam(value = "headerImg") MultipartFile headerImg,
                               @ApiParam(value = "生活照") @RequestParam(value = "photos") MultipartFile[] photos) throws IOException {

        log.info("上传的信息：邮箱={},用户名={},头像={},生活照={}", email, username, headerImg.getSize(), photos.length);
        if (!headerImg.isEmpty()) {
            // todo 保存到文件服务器,如阿里云的OSS中
            headerImg.transferTo(new File("D:\\a_myFile\\person_file\\tempFile\\" + headerImg.getOriginalFilename()));
        }
        if (photos.length > 0) {
            for (MultipartFile photo : photos) {
                if (!photo.isEmpty()) {
                    String originalFilename = photo.getOriginalFilename();
                    photo.transferTo(new File("D:\\a_myFile\\person_file\\tempFile\\" + originalFilename));
                }
            }
        }
        return "main";
    }
}
